package com.mhh.selary.backend.config;

import com.mhh.selary.backend.intercepter.LoginIntercepter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * 配置拦截器
 *
 * @author mhh
 * @date 2020-11-19 15:29:11
 */
@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport {

    @Autowired
    private LoginIntercepter loginIntercepter;

    private String webjarsUrl = "/webjars/**";

    /**
     * 定义登录拦截器，跨域拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginIntercepter).addPathPatterns("/**")
                .excludePathPatterns("/doc.html", "/swagger-resources/**", webjarsUrl, "/v2/**");
        super.addInterceptors(registry);
    }

    /**
     * 添加静态资源路径
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler(webjarsUrl)
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

}
